//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.events.BlogDraftEvent;
	import reader.events.MediaEvent;
	import reader.model.IDataProxy;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.BlogDraftVO;
	import reader.model.vo.CountVO;
	import reader.model.vo.MediaVO;
	import reader.services.IPostBlogAble;
	import reader.services.ServiceFactory;
	import reader.utils.StringUtils;
	import reader.view.components.BlogWriter;
	import reader.view.components.DashBoard;

	public class DashboardMediator extends ServiceAbleMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "DashboardMediator";
		
		public function get dashBoard():DashBoard
		{
			return this.viewComponent as DashBoard;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var draftProxy:IDraftProxy;
		private var dataProxy:IDataProxy;
		private var serviceProxy:IServiceProxy;
		private var postBlogService:IPostBlogAble;
		
		private var isDisplayed:Boolean;
		private var isInitialized:Boolean;
		
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function DashboardMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			draftProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.draftProxyName) as IDraftProxy;
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
		}
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.VIEW_START_UP,
									ApplicationFacade.COUNT_DATA,
									ApplicationFacade.COUNT_DRAFT,
									ApplicationFacade.FETCH_COUNTS,
									ApplicationFacade.WRITER_CLOSE,
									ApplicationFacade.WRITER_VIEW_CHANGE,
									ApplicationFacade.DRAFT_UNPUBED_OK,
									ApplicationFacade.DRAFT_PUBED_OK,
									ApplicationFacade.DRAFT_PUBLISHED,
									ApplicationFacade.MEDIA_ALL
									]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.VIEW_START_UP:
					if(String(notification.getBody()) == BlogWriter.VIEW_DASHBOARD)
					{
						startUp();
					}	
					break;	
				case ApplicationFacade.WRITER_CLOSE:
					clearReference();
					break;	
				case ApplicationFacade.FETCH_COUNTS:
					dataProxy.getCount();
					draftProxy.getCount();
					break;
				case ApplicationFacade.COUNT_DATA:
					var countData:CountVO = notification.getBody() as CountVO;
					dashBoard.ganeralTotalSubscribe.text = countData.totalSubscribes.toString();
					dashBoard.ganeralUnreadSubscribe.text = countData.unreadSubscribes.toString();
					dashBoard.ganeralTotalFeed.text = countData.totalFeeds.toString();
					dashBoard.ganeralTotalLabel.text = countData.totalLabels.toString();
					break;
				case ApplicationFacade.COUNT_DRAFT:
					var countDraft:CountVO = notification.getBody() as CountVO;
					dashBoard.ganeralPost.label = countDraft.totalPost.toString();
					dashBoard.ganeralDraft.label = countDraft.totalDraft.toString();
					dashBoard.ganeralMedia.label = countDraft.totalMedia.toString();
					break;
				case ApplicationFacade.WRITER_VIEW_CHANGE:
					if(String(notification.getBody()) == BlogWriter.VIEW_DASHBOARD)
					{
						isDisplayed = true;
						if(!isInitialized)
							startUp();
					}
					else
					{
						isDisplayed = false;
					}				
					break;
				case ApplicationFacade.DRAFT_UNPUBED_OK:
					if(dashBoard)
						dashBoard.blogDrafts = notification.getBody() as Vector.<BlogDraftVO>;
					break;
				case ApplicationFacade.DRAFT_PUBED_OK:
					if(dashBoard)
						dashBoard.publishedDrafts = notification.getBody() as Vector.<BlogDraftVO>;
					break;
				case ApplicationFacade.MEDIA_ALL:
					if(dashBoard)
						dashBoard.medias = notification.getBody() as Vector.<MediaVO>;
					break;	
				case ApplicationFacade.DRAFT_PUBLISHED:
					if(isDisplayed)
					{
						Alert.show(ReaderResource.instance.resource.blogWriterReadDraftPublished + notification.getType(), 
								ReaderResource.instance.resource.blogWriterReadDraftPublishedTitle, 4, dashBoard);
						draftProxy.getPublishDraft(10);
					}						
					break;			
			}
		}
		
		override public function startUp():void
		{
			isDisplayed = true;
			isInitialized = true;
			sendNotification(ApplicationFacade.FETCH_COUNTS);
			if(!dashBoard.blogDrafts)
				draftProxy.getUnPublishDraft(5);
			if(!dashBoard.publishedDrafts)
				draftProxy.getPublishDraft(10);	
			if(!dashBoard.medias)
				draftProxy.getAllMedia(3);
			dashBoard.addEventListener(BlogDraftEvent.OPEN_BLOG_DRAFT, onEditDraft_handler);
			dashBoard.addEventListener(BlogDraftEvent.VIEW_BLOG_DRAFT, onViewBlogDraft_handler);
			dashBoard.addEventListener(MediaEvent.VIEW_MEDIA, onViewMedia_handler);
			dashBoard.addEventListener(DashBoard.POST_BLOG, onPostBlog_handler, false, 0, true);
			dashBoard.addEventListener(DashBoard.SAVE_DRAFT, onSaveDraft_handler, false, 0, true);
		}
		
		override public function clearReference():void
		{
			readerModelLocator = null;
			draftProxy = null;
			dataProxy = null;
			serviceProxy = null;
			dashBoard.removeEventListener(BlogDraftEvent.OPEN_BLOG_DRAFT, onEditDraft_handler);
			dashBoard.removeEventListener(BlogDraftEvent.VIEW_BLOG_DRAFT, onViewBlogDraft_handler);
			dashBoard.removeEventListener(MediaEvent.VIEW_MEDIA, onViewMedia_handler);
			facade.removeMediator(NAME);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
	
		private function onEditDraft_handler(e:BlogDraftEvent):void
		{
			e.stopPropagation();
			sendNotification(ApplicationFacade.EDIT_DRAFT, e.draft);
		}
		
		private function onViewBlogDraft_handler(e:BlogDraftEvent):void
		{
			e.stopPropagation();
			sendNotification(ApplicationFacade.AIR_EXPLORER_OPEN, e.draft.link);
		}
		
		private function onViewMedia_handler(e:MediaEvent):void
		{
			e.stopPropagation();
			sendNotification(ApplicationFacade.AIR_EXPLORER_OPEN, e.media.uri);
		}
		
		private function onPostBlog_handler(e:Event):void
		{
			var blogDraftVO:BlogDraftVO = new BlogDraftVO();
			blogDraftVO.title = dashBoard.pressinput.text
			blogDraftVO.description = dashBoard.presscontent.text;
			postBlogService = ServiceFactory.getPostBlogService(dashBoard.blogService, serviceProxy, draftProxy, dashBoard, servicePool);
			postBlogService.newBlog(blogDraftVO);
		}
		
		private function onSaveDraft_handler(e:Event):void
		{
			var blogDraftVO:BlogDraftVO = new BlogDraftVO();
			var date:Date = new Date();
			
			blogDraftVO.title = StringUtils.removeExtraWhitespace(dashBoard.pressinput.text) == "" ? 
								ReaderResource.instance.resource.blogWriterUntitled + BlogDraftVO.formatDraftDate(date) : dashBoard.pressinput.text;
			blogDraftVO.description = dashBoard.presscontent.text;
			blogDraftVO.creatDate = date;
			blogDraftVO.isPubed = false;
			draftProxy.saveDraft(blogDraftVO);
		}
	}
}